﻿using System;
using System.Collections;
using System.Runtime.Serialization;
using System.Web;

namespace NWebMock
{
    public class HttpFileCollectionWrapper : IHttpFileCollection
    {
        #region Constructors

        public HttpFileCollectionWrapper(HttpFileCollection files)
        {
            if (files == null)
            {
                throw new ArgumentNullException("files");
            }

            this.innerFiles = files;
        }

        #endregion

        #region Fields

        private HttpFileCollection innerFiles = null;

        #endregion

        #region Properties

        public string[] AllKeys { get { return this.innerFiles.AllKeys; } }

        public int Count { get { return this.innerFiles.Count; } }

        public IHttpPostedFile this[int index]
        {
            get
            {
                HttpPostedFile item = this.innerFiles[index];

                if (item != null)
                {
                    return new HttpPostedFileWrapper(item);
                }
                else
                {
                    return null;
                }
            }
        }

        public IHttpPostedFile this[string name]
        {
            get
            {
                HttpPostedFile item = this.innerFiles[name];

                if (item != null)
                {
                    return new HttpPostedFileWrapper(item);
                }
                else
                {
                    return null;
                }
            }
        }

        public ICollection Keys { get { return this.innerFiles.Keys; } }

        #endregion

        #region Methods

        public void CopyTo(Array dest, int index)
        {
            this.innerFiles.CopyTo(dest, index);
        }

        public IHttpPostedFile Get(int index)
        {
            HttpPostedFile item = this.innerFiles.Get(index);

            if (item != null)
            {
                return new HttpPostedFileWrapper(item);
            }
            else
            {
                return null;
            }
        }

        public IHttpPostedFile Get(string name)
        {
            HttpPostedFile item = this.innerFiles.Get(name);

            if (item != null)
            {
                return new HttpPostedFileWrapper(item);
            }
            else
            {
                return null;
            }
        }

        public IEnumerator GetEnumerator()
        {
            return this.innerFiles.GetEnumerator();
        }

        public string GetKey(int index)
        {
            return this.innerFiles.GetKey(index);
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            this.innerFiles.GetObjectData(info, context);
        }

        public void OnDeserialization(Object sender)
        {
            this.innerFiles.OnDeserialization(sender);
        }

        #endregion

        #region ICollection Members

        void ICollection.CopyTo(Array dest, int index)
        {
            this.innerFiles.CopyTo(dest, index);
        }

        bool ICollection.IsSynchronized { get { return ((ICollection)this.innerFiles).IsSynchronized; } }

        object ICollection.SyncRoot { get { return ((ICollection)this.innerFiles).SyncRoot; } }

        #endregion
    }
}
